package com.zichuan.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zichuan
 * @version 1.0
 * @Package : com.zichuan.config
 * @Description : TODO
 * @Create on : 2025/2/17 19:20
 */
@Slf4j
@Configuration
public class RabbitmqConfig {

    @Value("${app.exchangeNormalName}")
    private String exchangeNormalName;

    @Value("${app.queueNormalName}")
    private String queueNormalName;

    @Value("${app.exchangeDlxName}")
    private String exchangeDlxName;

    @Value("${app.queueDlxName}")
    private String queueDlxName;

    @Bean
    public DirectExchange normalExchange(){
        return ExchangeBuilder.directExchange(exchangeNormalName).build();
    }

    @Bean
    public Queue normalQueue(){
        Map<String, Object> arguments = new HashMap<>();
        //arguments.put("x-message-ttl",15000);//设置队列的过期时间
        arguments.put("x-max-length",5);//队列的最大长度
        arguments.put("x-dead-letter-exchange",exchangeDlxName);//设置队列的死信交换机
        arguments.put("x-dead-letter-routing-key","error");//设置死信路由key
        return QueueBuilder
                .durable(queueNormalName)
                .withArguments(arguments)
                .build();

    }


    @Bean
    public Binding bindingNormal(DirectExchange normalExchange,Queue normalQueue){
        return BindingBuilder.bind(normalQueue).to(normalExchange).with("order");
    }

    //死信交换机
    @Bean
    public DirectExchange dlxExchange(){
        return ExchangeBuilder.directExchange("exchangeDlxName").build();
    }

    //死信队列
    @Bean
    public Queue dlxQueue(){
        return QueueBuilder
                .durable(queueDlxName)
                .build();

    }

    @Bean
    public Binding bindingDlx(DirectExchange dlxExchange,Queue dlxQueue){
        return BindingBuilder.bind(dlxQueue).to(dlxExchange).with("error");
    }

}
